package com.xlh.fgs;

import java.util.List;

import com.wz.activity.main.helper.MainInputBox;
import com.xlh.adapter.BtnsFgAdapter;
import com.xlh.bean.TitleBean;
import com.xlh.outside.R;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.GridView;

public class BtnsFg extends Fragment {

	public BtnsFg() {
		// TODO Auto-generated constructor stub
	}

	public static final String TAG = BtnsFg.class.getName();

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.btns_fg_layout, container, false);
	}

	private GridView mGridView;

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		mGridView = (GridView) view.findViewById(R.id.mGridView);
		if(ts != null) {
			adapter = new BtnsFgAdapter(getActivity(), ts);
			adapter.notifyDataSetChanged();
			mGridView.setAdapter(adapter);
		}

		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub

				String tag = ts.get(position).getmAction();
				if(itemClick != null) {
					itemClick.onSendTagToServerClick(tag);
				}
			}
		});

		mGridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id){

				String tag = ts.get(position).getmAction();
				//if(itemClick != null) {
				//itemClick.onSendTagToServerClick(tag);
				//}
				final  int butId = position;

				LayoutInflater inflater = getActivity().getLayoutInflater();
				View layout = inflater.inflate(R.layout.max_edit,null);

				final EditText maxEdit = (EditText)layout.findViewById(R.id.edt_max_edit);
				maxEdit.setText(ts.get(butId).getmText()+":"+ts.get(butId).getmAction());

				//Dialog alertDialog = new AlertDialog.Builder(getActivity());
				AlertDialog.Builder alertDialog=new AlertDialog.Builder(getActivity());  //先得到构造器
				alertDialog.setTitle("设置自定义按钮【"+ts.get(position).getId()+"】");
				alertDialog.setView(layout);

				alertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						try {
							//getActivity()
							//ts.get(butId).setmAction(maxEdit.getText().toString());
							//final String s ="sd";
							((OnItemClick)getActivity()).onSendTagToServerClick("clickLong:"+ts.get(butId).getId()+":"+maxEdit.getText().toString());
							//FUti.writeDateFile(MainActivity.this,"wzm_user_btn.txt", maxEdit.getText().toString().getBytes());
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});

				alertDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});


				alertDialog.create().show();

				return true;
			}
		});


	}

	//点击事件的回调接口
	public interface OnItemClick {
		void onSendTagToServerClick(String tag);
	}

	public void setOnItemClick(OnItemClick click) {
		this.itemClick = click;
	}
	private OnItemClick itemClick;

	private List<TitleBean> ts;

	public BtnsFgAdapter getAdapter() {
		return adapter;
	}

	public void setAdapter(BtnsFgAdapter adapter) {
		this.adapter = adapter;
	}

	private BtnsFgAdapter adapter;

	public void setBtnData(List<TitleBean> ts) {
		// TODO Auto-generated method stub
		this.ts = ts;
	}

	public  List<TitleBean>  getBtnData(){
		return this.ts;
	}

	//数据集是否可用
	public boolean availableData(){

		return this.ts!=null && this.ts.size() > 0;
//
//		if (this.ts!=null && this.ts.size()>0){
//			return true;
//		}else{
//			return false;
//		}
	}

}
